﻿using log4net;
using Microsoft.Xaml.Behaviors.Layout;
using StarryEdge.Core;
using StarryEdge.Core.Enums;
using StarryEdge.Core.Models;
using StarryEdge.WPF.Lang;
using StarryEdge.WPF.Resources.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using static log4net.Appender.RollingFileAppender;

namespace StarryEdge.Domains
{
    public class StarryEdgeLog : IStarryEdgeLog
    {
        private AsyncObservableCollection<LogInfoModel> _runLog { get; set; }
        private AsyncObservableCollection<LogInfoModel> _operationLog { get; set; }
        private AsyncObservableCollection<MesLogModel> _mesLog { get; set; }
        private ILog _run = LogManager.GetLogger("message");
        private ILog _operation = LogManager.GetLogger("operation");
        public StarryEdgeLog()
        {
            _runLog = new AsyncObservableCollection<LogInfoModel>();
            _operationLog = new AsyncObservableCollection<LogInfoModel>();
            _mesLog = new AsyncObservableCollection<MesLogModel>();
        }

        public void LogControls(string message, string operation, LogLevel logLevel = LogLevel.Info)
        {
            OperationAdd($"{LangManager.Instance.Lang["Operator"]}：{operation.Replace("\r\n", "<=>")} {LangManager.Instance.Lang["Log"]}：{message} ", logLevel);
        }

        public void LogRun(string message, LogLevel logLevel = LogLevel.Info)
        {
            switch (logLevel)
            {
                case LogLevel.Info:
                    _run.Info(message);
                    break;
                case LogLevel.Success:
                    _run.InfoFormat(message);
                    break;
                case LogLevel.Warning:
                    _run.Warn(message);
                    break;
                case LogLevel.Error:
                    _run.Error(message);
                    break;
            }
            LogAdd(message, logLevel);
        }

        private void LogAdd(string str, LogLevel levelType)
        {
            try
            {
                _runLog.Insert(0, new LogInfoModel()
                {
                    Level = levelType,
                    Message = str,
                    Time = DateTime.Now.ToString("HH:mm:ss:fff"),
                });
                if (_runLog.Count > 800)
                {
                    _runLog.RemoveAt(800);
                }
            }
            catch (Exception ex)
            {
                _run.Error($"LogAdd 添加日志时发生异常：{ex}");
            }
        }

        private void OperationAdd(string str, LogLevel levelType)
        {
            try
            {
                _operationLog.Insert(0, new LogInfoModel()
                {
                    Level = levelType,
                    Message = str,
                    Time = DateTime.Now.ToString("HH:mm:ss:fff"),
                });
                if (_operationLog.Count > 800)
                {
                    _operationLog.RemoveAt(800);
                }
            }
            catch (Exception ex)
            {
                _operation.Error($"Operation 添加日志时发生异常：{ex}");
            }
        }

        public IEnumerable<LogInfoModel> GetRunLog()
        {
            return _runLog;
        }

        public IEnumerable<LogInfoModel> GetOperation()
        {
            return _operationLog;
        }

        public void LogMes(string interfaceName, DateTime callTime, long expend, string request, string respond, string interfaceLog, string barcode = null)
        {
            ILog mes = LogManager.GetLogger(interfaceLog);
            try
            {
                _mesLog.Insert(0, new MesLogModel()
                {
                    RunTime = callTime,
                    Request = request,
                    Respond = respond,
                    BarCode = barcode,
                    InterfaceName = interfaceName,
                    Expend = expend,
                });
                mes.Info($"{interfaceName},${barcode},${callTime:yyyy/MM/dd HH:mm:ss.fff},${DateTime.Now:yyyy/MM/dd HH:mm:ss.fff},${expend},\"${request.Replace("\"", "\"\"")}\",\"${respond.Replace("\"", "\"\"")}\"");
                if (_mesLog.Count > 800)
                {
                    _mesLog.RemoveAt(800);
                }
            }
            catch (Exception ex)
            {
                _run.Error($"MesLog 添加日志信息时发生异常：{ex}");
            }
        }

        public IEnumerable<MesLogModel> GetMesLog()
        {
            return _mesLog;
        }
    }
}
